package com.aike.service;

import com.aike.dao.TaskMapper;
import com.aike.dao.UserMapper;
import com.aike.pojo.*;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskServiceImpl implements TaskService{

    private UserMapper userMapper;

    private TaskMapper taskMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public void setTaskMapper(TaskMapper taskMapper) {
        this.taskMapper = taskMapper;
    }

    public String getTaskList(Integer userId) {
        JSONObject object = new JSONObject();
        Map<String, Object> map = new HashMap<String, Object>();
        User user = userMapper.getUserById(userId);
        map.put("user", user);

        //空、学生、管理员
        if(user == null || user.getType() == 1 || user.getType() == 3)
        {
            List<TaskListForStudent> list = taskMapper.getTaskListForStudent(userId);
            map.put("list", list);
        }
        //老师
        else if(user.getType() == 2)
        {
            List<TaskListForTeacher> list = taskMapper.getTaskListForTeacher(userId);
            map.put("list", list);
        }

        object.put("data", map);
        object.put("meta", new Meta(200, "获取成功"));

        return object.toJSONString();

    }

    public String getTaskShow(Integer taskId, Integer userId) {
        JSONObject object = new JSONObject();
        Map<String, Object> map = new HashMap<String, Object>();
        User user = userMapper.getUserById(userId);
        map.put("user", user);

        //学生
        if(user.getType() == 1)
        {
            TaskShowForStudent taskShowForStudent = taskMapper.getTaskShowForStudent(taskId, userId);
            map.put("show", taskShowForStudent);
        }
        else if(user.getType() == 2 || user.getType() == 3)
        {
            TaskShowForTeacher taskShowForTeacher = taskMapper.getTaskShowForTeacher(taskId);
            map.put("show", taskShowForTeacher);
        }

        object.put("data", map);
        object.put("meta", new Meta(200, "获取成功"));

        return object.toJSONString();
    }

    public String deleteTaskById(Integer taskId) {
        JSONObject object = new JSONObject();
        taskMapper.deleteGradeByTaskId(taskId);
        int v = taskMapper.deleteTaskById(taskId);
        if(v > 0)
        {
            object.put("data", "null");
            object.put("meta", new Meta(200, "删除成功"));
        }
        else
        {
            object.put("data", "null");
            object.put("meta", new Meta(400, "删除失败"));
        }

        return object.toJSONString();
    }

    public String addNewTask(Integer userId, String taskTitle, String taskMain) {
        JSONObject object = new JSONObject();
        int v = taskMapper.addNewTask(userId, taskTitle, taskMain);
        if(v > 0)
        {
            object.put("data", "null");
            object.put("meta", new Meta(200, "增加成功"));
        }
        else
        {
            object.put("data", "null");
            object.put("meta", new Meta(400, "增加失败"));
        }

        return object.toJSONString();
    }

    public String updateTaskById(Integer taskId, String taskTitle, String taskMain) {
        JSONObject object = new JSONObject();
        int v = taskMapper.updateTaskById(taskId, taskTitle, taskMain);
        if(v > 0)
        {
            object.put("data", "null");
            object.put("meta", new Meta(200, "更新成功"));
        }
        else
        {
            object.put("data", "null");
            object.put("meta", new Meta(400, "更新失败"));
        }

        return object.toJSONString();
    }
}
